package com.example.cmh.controller;

import com.example.cmh.common.PageResult;
import com.example.cmh.common.Result;
import com.example.cmh.pojo.Meeting;
import com.example.cmh.service.MeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/meetings")
public class MeetingController {

    @Autowired
    private MeetingService meetingService;

    @GetMapping("/list")
    public Result<List<Meeting>> getMeetings(
            @RequestParam(name = "meetingName", required = false) String meetingName,
            @RequestParam(name = "creatPerson", required = false) String creatPerson,
            @RequestParam(name = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @RequestParam(name = "currentUserId", required = false) Integer currentUserId) {
        return meetingService.getMeetingList(meetingName, creatPerson, startTime, currentUserId);
    }

    @GetMapping("/list-page")
    public Result<PageResult<Meeting>> getMeetingsWithPage(
            @RequestParam(name = "meetingName", required = false) String meetingName,
            @RequestParam(name = "creatPerson", required = false) String creatPerson,
            @RequestParam(name = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @RequestParam(name = "currentUserId", required = false) Integer currentUserId,
            @RequestParam(name = "current", required = false, defaultValue = "1") Integer current) {
        return meetingService.getMeetingListWithPage(meetingName, creatPerson, startTime, currentUserId, current);
    }

    @GetMapping("/list-all")
    public Result<List<Meeting>> getAllMeetings(
            @RequestParam(name = "meetingName", required = false) String meetingName,
            @RequestParam(name = "creatPerson", required = false) String creatPerson,
            @RequestParam(name = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime) {
        // 调试接口：查看所有会议，不受权限限制
        return meetingService.getAllMeetings(meetingName, creatPerson, startTime);
    }

    @PostMapping("/create")
    public Result<String> createMeeting(@RequestBody Meeting meeting, @RequestParam(name = "currentUserId", required = false) Integer currentUserId) {
        return meetingService.createMeeting(meeting, currentUserId);
    }

    @PutMapping("/update/{isAdmin}")
    public Result<String> updateMeeting(@PathVariable Boolean isAdmin,@RequestBody Meeting meeting) {
        return meetingService.updateMeeting(isAdmin,meeting);
    }

    @DeleteMapping("/delete")
    public Result<String> deleteMeeting(@RequestParam(name = "meetingId") Integer meetingId) {
        return meetingService.deleteMeeting(meetingId);
    }

    @PostMapping("/review")
    public Result<String> reviewMeeting(@RequestParam(name = "meetingId", required = false) Integer meetingId, @RequestParam(name = "status", required = false) Integer status) {
        if (meetingId == null) {
            return Result.error("会议ID不能为空");
        }
        if (status == null) {
            return Result.error("审核状态不能为空");
        }
        return meetingService.reviewMeeting(meetingId, status);
    }
} 